package com.tanhua.server.controller;


import com.tanhua.model.domian.UserInfo;
import com.tanhua.model.vo.CountVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.server.QuestionService;
import com.tanhua.server.server.SettingsService;
import com.tanhua.server.server.TanhuaService;
import com.tanhua.server.server.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
@Slf4j
/**
 * 我的资料相关
 */
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private TanhuaService tanhuaService;


    /**
     * 根据ID查询到用户数据
     * @param
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity users(Long userID){

        //判断从前端传来的ID是否为NUll
        if (userID == null) {
           userID = UserHolder.getUserId();
        }
        log.info("userID={}",userID);
        UserInfo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 修改用户数据 userinfo
     * @param userInfo
     * @param
     * @return
     */
    @PutMapping
    public ResponseEntity UpdateUserInfo(@RequestBody UserInfo userInfo){
        log.info("userInfo={}",userInfo.toString());
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更换头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        Long userId = UserHolder.getUserId();
        userInfoService.updatehead(headPhoto,userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
          CountVo vo = tanhuaService.likeLoveCounts();
          return ResponseEntity.ok(vo);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @GetMapping("/friends/{type}")   //4 = 谁看过我 , 1 = 互相喜欢,2=喜欢，3=粉丝
    public ResponseEntity friends(@PathVariable("type") String type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  String nickname){
        log.info("type={},page={},pagesize={},nickname={}",type,page,pagesize,nickname);
        PageResult pr = userInfoService.friends(type,page,pagesize,nickname);
        return ResponseEntity.ok(pr);
    }

    /**
     * 喜欢 - 取消
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity cancelLikes(@PathVariable Long uid){
            log.info("uid={}",uid);
        tanhuaService.cancelLikes(uid);
            return ResponseEntity.ok(null);
    }

    /**
     * 粉丝 喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable Long uid){
        tanhuaService.fansLike(uid);
        return ResponseEntity.ok(null);
    }

}
